﻿class MuonsachesController < ApplicationController
  # GET /muonsaches
  # GET /muonsaches.json
  def index
    @muonsaches = Muonsach.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @muonsaches }
    end
  end

  # GET /muonsaches/1
  # GET /muonsaches/1.json
  def show
    @muonsach = Muonsach.find(params[:id])
    if(session[:user_id])
      @user = User.find(session[:user_id])
    else
      @user = -1
    end
  @booktable = Booktable.find(@muonsach.bookId)
  @booktable.quantity = @booktable.quantity - 1
  @booktable.save
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @muonsach }
    end
  end

  # GET /muonsaches/new
  # GET /muonsaches/new.json
  def new
    @muonsach = Muonsach.new
    @check =Muonsach.all
    @booktable = Booktable.find(params[:id])
 if(session[:user_id])
      @user = User.find(session[:user_id])
    else
      @user = -1
    end
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @muonsach }
    end
  end

  # GET /muonsaches/1/edit
  def edit
    @muonsach = Muonsach.find(params[:id])
  end

  # POST /muonsaches
  # POST /muonsaches.json
  def create
    @muonsach = Muonsach.new(params[:muonsach])

    respond_to do |format|
      if @muonsach.save
        format.html { redirect_to @muonsach, notice: "#{t(:statusborrow)}" }
        format.json { render json: @muonsach, status: :created, location: @muonsach }
      else
        format.html { render action: "new" }
        format.json { render json: @muonsach.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /muonsaches/1
  # PUT /muonsaches/1.json
  def update
    @muonsach = Muonsach.find(params[:id])

    respond_to do |format|
      if @muonsach.update_attributes(params[:muonsach])
        format.html { redirect_to @muonsach, notice: "#{t(:sttupdbor)}" }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @muonsach.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /muonsaches/1
  # DELETE /muonsaches/1.json
  def destroy
    @muonsach = Muonsach.find(params[:id])
     @booktable = Booktable.find(@muonsach.bookId)
  @booktable.quantity = @booktable.quantity + 1
  @booktable.save
    @muonsach.destroy

    respond_to do |format|
      format.html { redirect_to muonsaches_url }
      format.json { head :no_content }
    end
  end
end
